//
//  OrderManagerListView.m
//  YongChuanTradingArea
//
//  Created by 李冰 on 2017/9/15.
//  Copyright © 2017年 李冰. All rights reserved.
//

#import "OrderManagerListView.h"
#import "OrderManagerTableCell.h"

static NSString * const OrderManagerTableCellIde = @"OrderManagerTableCellIde";
@interface OrderManagerListView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, assign) NSInteger pageIndex;
@property (nonatomic, assign) NSInteger pageSize;
@property (strong, nonatomic) UITableView *tableView;

@end

@implementation OrderManagerListView {

    ManagerOrderType _type;
    BOOL _requestSuccess;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {

    return [self initWithFrame:CGRectZero type:ManagerOrderTypeCancel];
}

- (instancetype)initWithFrame:(CGRect)frame {

    return [self initWithFrame:CGRectZero type:ManagerOrderTypeCancel];
}

-(instancetype)initWithFrame:(CGRect)frame type:(ManagerOrderType)type {

    if (self = [super initWithFrame:frame])
    {
     
        self.pageSize = 20;
        self.pageIndex = 1;
        _type = type;
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.rowHeight = 100;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerNib:[UINib nibWithNibName:@"OrderManagerTableCell" bundle:nil] forCellReuseIdentifier:OrderManagerTableCellIde];
        self.tableView.tableFooterView = [UIView new];
        [self addSubview:_tableView];
        _tableView.translatesAutoresizingMaskIntoConstraints = NO;
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_tableView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tableView)]];
        [self addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_tableView]|" options:0 metrics:nil views:NSDictionaryOfVariableBindings(_tableView)]];
        
        WEAKSELF;
        self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            
            weakSelf.pageIndex = 1;
            weakSelf.tableView.mj_footer.hidden = YES;
            [self _requestData];
        }];
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            
            weakSelf.tableView.mj_header.hidden = YES;
            [self _requestData];
        }];
        
    }
    
    return self;
}

- (void)willShow {
    
    if (!_requestSuccess)
    {
        [self.tableView.mj_header beginRefreshing];
    }
}

- (void) _requestData {
    
    NSMutableDictionary *param = [NSMutableDictionary new];
    
    if ([LBUserManage defaultManager].user_id)
    {
        [param setObject:[LBUserManage defaultManager].sellerId forKey:@"sellerId"];
    }
    [param setObject:[NSString stringWithFormat:@"%ld", self.pageIndex] forKey:@"pageindex"];
    [param setObject:[NSString stringWithFormat:@"%ld", self.pageSize] forKey:@"pagesize"];
    [param setObject:[NSString stringWithFormat:@"%ld", _type] forKey:@"type"];
//    GetSellerOrderList?sellerId={sellerId}&type={type}&pageindex={pageindex}&pagesize={pagesize}
    [LBNetworkingInstance requestGET:storeOrderList parameters:param showHUD:YES success:^(id  _Nonnull responseObject, NSInteger code, NSString * _Nonnull message) {
        
        [self _endRefresh];
        if (code == YCNetworkingStatusCodeSuccess || code == YCNetworkingStatusCodeNoneData)
        {
            NSArray *array = [NSArray yy_modelArrayWithClass:OrderManagerListModel.class json:responseObject[@"Data"]];
            
            if (self.pageIndex == 1)
            {
                [self.dataArray removeAllObjects];
            }
            
            [self.dataArray addObjectsFromArray:array];
            
            if (array.count < self.pageSize)
            {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else
            {
                self.pageIndex++;
            }
            
            _requestSuccess = YES;
            [self.tableView reloadData];
            
        }
    }failure:^(NSError * _Nonnull err) {
        
        [self _endRefresh];
    }];
}

- (void) _endRefresh {
    
    [self.tableView.mj_footer endRefreshing];
    [self.tableView.mj_header endRefreshing];
    self.tableView.mj_header.hidden = NO;
    self.tableView.mj_footer.hidden = NO;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    OrderManagerListModel *model = self.dataArray[indexPath.item];
    OrderManagerTableCell *cell = [tableView dequeueReusableCellWithIdentifier:OrderManagerTableCellIde forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (NSMutableArray *)dataArray {

    if (_dataArray) return _dataArray;
    
    _dataArray = [NSMutableArray new];
    return _dataArray;
}

@end
